<?php
require_once 'helpers.php';

class DatemodsController extends Zend_Controller_Action
{   
    public function indexAction()
    {
        $this->view->title = 'Change Your "Uploaded" Dates';
        
        $form = new DatemodsForm();
        $form->submit->setLabel('Get List of Photos');
        $this->view->form = $form;
                
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();

            if ($form->isValid($formData)) {

                $myDatemodsSearch = new DatemodsSearch($form);

                session_start();                
                if (isset($_SESSION['datemodsearch'])) {
                    unset($_SESSION['datemodsearch']);
                }
                
                $_SESSION['datemodsearch'] = $myDatemodsSearch;
                //TO DO We should get our base url somehow
                header("Location: " . "/flickrocd/datemods/status");
            }
        }
        else {
            if (isset($_SESSION['datemodsearch'])) {
                unset($_SESSION['datemodsearch']);
            }
            //We show the form in the default view
        }
    }
    public function statusAction()
    {
        session_start();
        if (!isset($_SESSION['datemodsearch'])){
            header("Location: " . "/flickrocd/datemods");
        }
        else {
            $myDatemodsSearch = $_SESSION['datemodsearch'];
        }
		
        $myDatemodsSearch->doNext();
        
        if(!$myDatemodsSearch->done) {        
            $this->view->status_message = $myDatemodsSearch->getStatus();
            $this->view->title = "Getting Photos For Date Changing";
            $this->_helper->viewRenderer('status');
        }
        else {//TODO Move this to resutlsAction
            header("Location: " . "/flickrocd/datemods/results");
        }
    }
    
    public function resultsAction()
    {
        session_start();
        if (!isset($_SESSION['datemodsearch'])) {
            header("Location: " . "/flickrocd/datemods");
        }
        else {
            $myDatemodsSearch = $_SESSION['datemodsearch'];
        }
        if(count($myDatemodsSearch->results) > 0) {
            $this->view->results = $myDatemodsSearch->results;
        }
        else {
            $this->view->user_message = "No photos found.";
        }
        $this->view->title = "Current Photo Dates";
    }
    public function updateAction()
    {
        session_start();
        if (!isset($_SESSION['datemodsearch'])) {
            header("Location: " . "/flickrocd/datemods");
        }
        else {
            $myDatemodsSearch = $_SESSION['datemodsearch'];
        }
        
        // We hit this controller twice. Once to confirm, once to do the
        // actual deletion.
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if (isset($formData['confirmed'])){

                $this->view->user_message = $myDatemodsSearch->updateDates();
                $this->view->title = "Updated";
                // Clear our search data
                unset($_SESSION['datemodsearch']);
            }
            else{
                $this->view->resultscount = count($myDatemodsSearch->results);
                 
                // Create confirmation form
                $form = new DatemodsConfirmForm($formData);
                $this->view->ask = true; 
                $this->view->title = "Confirm Date Changes";
                $this->view->form = $form;              
            }
        }
        else {
            header("Location: " . "/flickrocd/duplicates");
        } 
    }
}

?>